package com.qdev.apps.core.persistence;

import java.util.List;

/**
 * @author Quake Wang
 * @since 2004-7-30
 * @version $Revision: 1.2 $
 */
public class PaginationSupport {
    private List items;
    private int totalCount;
    private int startIndex;
    private int countOnEachPage;
    public static int DEFAULT_COUNT_ON_EACH_PAGE = 10;

    public PaginationSupport() {
        this(DEFAULT_COUNT_ON_EACH_PAGE);
    }

    public PaginationSupport(int countOnEachPage) {
        startIndex = 0;
        if (countOnEachPage < 1) {
            throw new IllegalArgumentException("Count should be greater than zero!");
        } else {
            this.countOnEachPage = countOnEachPage;
        }
    }

    public PaginationSupport(List items, int startIndex, int totalCount, int countOnEachPage) {
        this.items = items;
        this.startIndex = startIndex;
        this.totalCount = totalCount;
        this.countOnEachPage = countOnEachPage;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public void setCountOnEachPage(int countOnEachPage) {
        this.countOnEachPage = countOnEachPage;
    }

    public List getItems() {
        return items;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getEndIndex() {
        int endIndex = getStartIndex() + countOnEachPage;
        if (endIndex > totalCount)
            return totalCount;
        else
            return endIndex;
    }

    public int getStartIndex() {
        if (startIndex > totalCount)
            return totalCount;
        if (startIndex < 0)
            return 0;
        else
            return startIndex;
    }

    public int getNextIndex() {
        return getNextStartIndexes()[0];
    }

    public int getPreviousIndex() {
        int previousIndexes[] = getPreviousStartIndexes();
        return previousIndexes[previousIndexes.length - 1];
    }

    public int[] getNextStartIndexes() {
        int index = getEndIndex();
        if (index == totalCount)
            return null;
        int count = (totalCount - index) / countOnEachPage;
        if ((totalCount - index) % countOnEachPage > 0)
            count++;
        int result[] = new int[count];
        for (int i = 0; i < count; i++) {
            result[i] = index;
            index += countOnEachPage;
        }

        return result;
    }

    public int[] getPreviousStartIndexes() {
        int index = getStartIndex();
        if (index == 0)
            return null;
        int count = index / countOnEachPage;
        if (index % countOnEachPage > 0)
            count++;
        int result[] = new int[count];
        for (int i = count - 1; i > 0; i--) {
            index -= countOnEachPage;
            result[i] = index;
        }

        return result;
    }
    
    public int getCountOnEachPage() {
        return countOnEachPage;
    }

    public void setItems(List items) {
        this.items = items;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
        
}